import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
public class  SimpleChatInterface extends JFrame {
    public Socket p ;
    public DatePacket d ;
    OutputStream outputStream;
    Roompower zhe;
    JFrame frame;
    JTextArea chatArea;
    private JTextField messageField;
    private JButton sendButton;
    private JButton exitButton;
    private DefaultListModel<String> chatListModel;
    private JList<String> chatList;
    public SimpleChatInterface(Socket p, DatePacket d, OutputStream outputStream, Roompower zhe) {

        this.p = p;
        this.d = d;
        this.outputStream = outputStream;
        this.zhe = zhe;
        frame = new JFrame(zhe.roomname+"     "+zhe.room);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null); // 窗口居中显示
        chatArea = new JTextArea();
        messageField = new JTextField(20);
        sendButton = new JButton("发送");
        String wei = "";
        if(zhe.power == 1){
            wei = "销毁房间";

        }else if(zhe.power == 0){
            wei = "退出房间";
        }
        exitButton = new JButton(wei);
        chatListModel = new DefaultListModel<>();
        chatList = new JList<>(chatListModel);
        setupGUI();
        setupEvents();
        pack();
    }
    public void end(){
        frame.dispose();
    }
    private void setupGUI() {
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null); // 窗口居中显示
        frame.setLayout(new BorderLayout());
        JScrollPane chatPane = new JScrollPane(chatList);
        chatPane.setPreferredSize(new Dimension(350, 150));
        frame.add(chatPane, BorderLayout.CENTER);
        JPanel southPanel = new JPanel();
        southPanel.add(messageField);
        southPanel.add(sendButton);
        frame.add(southPanel, BorderLayout.SOUTH);
        JPanel eastPanel = new JPanel();
        eastPanel.add(exitButton);
        frame.add(eastPanel, BorderLayout.EAST);
        frame.add(new JScrollPane(chatArea), BorderLayout.CENTER); // 中部区域，聊天显示和垂直滚动条
        frame.setVisible(false);
    }
    private void setupEvents() {
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String message = messageField.getText();
                int length = message.length();;
                if (length == 0 || length >= 250) {
                } else {
                    DatePacket  G=new DatePacket();
                    G.direct = 3;
                    G.room = zhe.room;
                    G.name= d.name;
                    G.message = message;
                    try {
                        ObjectOutputStream oos = new ObjectOutputStream(outputStream);
                        oos.writeObject(G);
                        oos.flush();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    messageField.setText(""); // 清空输入框，准备输入下一条消息
                }
            }
        });
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                d.direct = zhe.power + 4;
                d.room = zhe.room;
                try {
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(d);
                    objectOutputStream.flush();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }
}